package org.southyin.demo01;

import org.southyin.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class JDBCDemo02 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtils.getInstance().getConnection();
            // 使用 ? 占位
            // 1.提高效率
            // 2.防止 SQL 注入
            String sql = "select * from emp where ename = ? and job = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,"mike");
            ps.setString(2,"salesman");
            rs = ps.executeQuery();
            if (rs.next()) {
                long id = rs.getLong("id");
                String ename = rs.getString("ename");
                String job = rs.getString("job");
                Date birthday = new Date(rs.getDate("birthday").getTime());
                System.out.println(id + "," + ename + "," + job + "," + birthday);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 6.关闭连接
            DBUtils.getInstance().close(rs);
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(rs);
        }
    }
}
